﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace XQ.Framework.core
{
    /// <summary>
    /// 正则表达式公共类
    /// </summary>
    public class RegexHelper
    {
        /// <summary>
        /// 正则表达式
        /// </summary>
        //邮件正则表达式
        //public const string EMAIL = @"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*";
        public const string EMAIL = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
        //qq号
        public const string QQ = @"[1-9][0-9]{4,}";
        //固话号正则表达式
        public const string PHONE = @"^(\d{3,4}-?)?\d{7,8}$";
        //手机号正则表达式
        public const string MOBILEPHONE = @"^(13|15|18)[0-9]{9}$";
        //URL
        public const string URL = @"[a-zA-z]+://[^s]*";
        //ip
        public const string IP = @"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
        //public const string IP=@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"；
        public const string CHINEASE = @"[u4e00-u9fa5]";
        //身份证
        public const string CERTNUMBER = @"d{15}|d{18}";
        //账号
        public const string ACCOUNT = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
        //日期正则表达式
        public const string DATE = @"(\d{4})-(\d{1,2})-(\d{1,2})";
        //整数
        public const string INT = @"^-?[1-9]\d*$";
        //数值(包括整数和小数)正则表达式
        public const string NUMBER = @"^[-]?[0-9]+(\.[0-9]+)?$";
        //邮政编码正则表达式
        public const string ZIPCODE = @"^\d{6}$";

        public const string META = "<meta([^<]*)charset=([^<]*)[\"']";


        /// <summary>
        /// 检测是否匹配
        /// </summary>
        /// <param name="str"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsMatch(string str, string pattern)
        {
            return IsMatch(str, pattern, RegexOptions.None);
        }
        public static bool IsMatch(string str, string pattern, RegexOptions options)
        {
            return Regex.IsMatch(str, pattern, options);
        }
        public static Match GetMatch(string str,string pattern)
        {
            return GetMatch(str,pattern,RegexOptions.None);
        }
        public static Match GetMatch(string str,string pattern,RegexOptions options)
        {
            return Regex.Match(str,pattern,options);
        }
        /// <summary>
        /// 从字符串中找到匹配的字符
        /// </summary>
        /// <param name="str">原始字符</param>
        /// <param name="pattern">正则</param>
        /// <param name="spliter">分隔符</param>
        /// <returns></returns>
        public static List<string> FindString(string str, string pattern)
        {
            str = ConvertHelper.getString(str);
            pattern = ConvertHelper.getString(pattern);
            try
            {
                List<string> list = new List<string>();
                Regex regex = new Regex(pattern);
                Match matcher = regex.Match(str);
                while (matcher.Success)
                {
                    list.Add(matcher.Groups[0].Value);
                    matcher = matcher.NextMatch();
                }
                return list;
            }
            catch (Exception err)
            {
                return null;
            }
        }

    }
}